package com.eastfair.imaster.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.*;
import com.eastfair.imaster.entity.SpeakerListDetails;
import com.eastfair.imaster.service.SpeakerListDetailsService;
import com.eastfair.imaster.vo.SpeakerListDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 嘉宾列表详情
 * </p>
 *
 * @author zjn
 * @date 2021-09-02
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/speakerListDetails")
@Api(value = "SpeakerListDetails", tags = "嘉宾列表详情")
public class SpeakerListDetailsController extends SuperController<SpeakerListDetailsService, Long, SpeakerListDetails, SpeakerListDetailsPageQuery, SpeakerListDetailsSaveDTO, SpeakerListDetailsUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private SpeakerListDetailsService speakerListDetailsService;

    @Override
    public void handlerResult(IPage<SpeakerListDetails> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<SpeakerListDetails> speakerListDetailsList = list.stream().map((map) -> {
            SpeakerListDetails speakerListDetails = SpeakerListDetails.builder().build();
            //TODO 请在这里完成转换
            return speakerListDetails;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(speakerListDetailsList));
    }

    /**
     * 新增列表嘉宾
     *
     * @param speakerListDetailsDTOList 列表嘉宾集合
     * @return 是否新增成功
     */
    @ApiOperation(value = "新增列表嘉宾", notes = "新增列表嘉宾")
    @PostMapping("/addSpeakerListDetailsList")
    public R<Boolean> addSpeakerListDetailsList(@RequestBody SpeakerListDetailsRequestDTO speakerListDetailsDTOList) {
        if (Objects.isNull(speakerListDetailsDTOList) || CollectionUtil.isEmpty(speakerListDetailsDTOList.getSpeakerListDetailsDTOList())) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }
        return speakerListDetailsService.addSpeakerListDetailsList(speakerListDetailsDTOList.getSpeakerListDetailsDTOList());
    }

    /**
     * 删除列表嘉宾
     *
     * @param id 主键
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除列表嘉宾", notes = "删除列表嘉宾")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteSpeakerListDetails(@PathVariable("id") Long id) {
        return speakerListDetailsService.deleteSpeakerListDetails(id);
    }

    /**
     * 通过id获取嘉宾分页详情
     *
     * @param params 嘉宾列表id，分页
     * @return 分页嘉宾列表详情
     */
    @ApiOperation(value = "通过id获取嘉宾分页详情", notes = "通过id获取嘉宾分页详情")
    @PostMapping("/queryPageDetails")
    public R<Page<SpeakerListDetailsVO>> queryPageDetails(@RequestBody PageParams<SpeakerListDetailsPageQuery> params) {
        return speakerListDetailsService.queryPageDetails(params);
    }

    /**
     * 通过id获取嘉宾详情
     *
     * @param speakerListDetailsDTO 嘉宾列表id
     * @return 嘉宾列表详情
     */
    @ApiOperation(value = "通过id获取嘉宾分页详情", notes = "通过id获取嘉宾分页详情")
    @PostMapping("/queryListDetails")
    public R<List<SpeakerListDetailsVO>> queryListDetails(@RequestBody SpeakerListDetailsDTO speakerListDetailsDTO) {
        return speakerListDetailsService.queryListDetails(speakerListDetailsDTO);
    }

}
